package com.hoongs.v1.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.hoongs.v1.utils.DateUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Repository;

import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Repository
@TableName(value = "order_product")
public class OrderProduct {

    @TableId(type = IdType.ASSIGN_UUID)
    private String id;

    @NotBlank(message = "商品信息不能为空")
    @TableField(value = "product_id")
    private String productId;      // 商品id

    @NotBlank(message = "订单信息不能为空")
    @TableField(value = "order_id")
    private String orderId;      // 订单id

    @NotBlank(message = "下单人信息不能为空")
    @TableField(value = "user_id")
    private String userId;

    @NotBlank(message = "购买商品数量不能为空")
    private BigDecimal number;      // 商品数量

    @NotBlank(message = "购买商品价格不能为空")
    private BigDecimal price;      // 商品价格

    @NotBlank(message = "订单编号不能为空")
    @TableField(value = "order_code")
    private String orderCode;      // 订单编号

    @JsonFormat(pattern = DateUtil.PATTERN_DATETIME)
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private LocalDateTime createTime;       // 发布时间

    @JsonFormat(pattern = DateUtil.PATTERN_DATETIME)
    @DateTimeFormat(pattern = DateUtil.PATTERN_DATETIME)
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;    // 更新时间

    public static OrderProduct fromCart(Cart cart, Order order) {
        OrderProduct orderProduct = new OrderProduct();
        orderProduct.setProductId(cart.getProductId());
        orderProduct.setNumber(cart.getNumber());
        orderProduct.setOrderId(order.getId());
        orderProduct.setOrderCode(order.getOrderCode());
        orderProduct.setUserId(order.getUserId());
        return orderProduct;
    }

}
